﻿using ConfigCreate.Controls;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace ConfigCreate.Model
{
    /// <summary>
    /// 大类别数据
    /// </summary>
    public class HeaderData
    {
        internal static long header_id = 0;

        /// <summary>
        /// 重置大类别ID
        /// </summary>
        public static void ResetHeaderId()
        {
            header_id = 0;
        }

        private ConcurrentDictionary<string, CreateControlGroup> list = new ConcurrentDictionary<string, CreateControlGroup>();
        private ConcurrentDictionary<string, DockPanelData> dock_panel_list = new ConcurrentDictionary<string, DockPanelData>();

        /// <summary>
        /// 初始化大类别数据
        /// </summary>
        /// <param name="header_name"></param>
        public HeaderData(string header_name)
        {
            HeaderName = header_name;

            HeaderId = System.Threading.Interlocked.Increment(ref header_id);
        }

        /// <summary>
        /// 大类别名
        /// </summary>
        public string HeaderName { get; set; }
        /// <summary>
        /// 大类别ID
        /// </summary>
        public long HeaderId { get; }
        /// <summary>
        /// 大类别索引
        /// </summary>
        public int HeaderIndex { get; set; } = int.MaxValue;

        /// <summary>
        /// 权限列表
        /// </summary>
        internal ConcurrentDictionary<RolePermissionType, HashSet<string>> RolePermission { get; } = new ConcurrentDictionary<RolePermissionType, HashSet<string>>();

        internal bool CheckRolePermission(RolePermissionType type, string? role_name)
        {
            if (string.IsNullOrEmpty(role_name))
                return true;
            if (!RolePermission.TryGetValue(type, out var tl))
                return true;
            return tl.Contains(role_name ?? string.Empty);
        }

        /// <summary>
        /// 添加组
        /// </summary>
        /// <param name="group_name">组名</param>
        /// <param name="group_index">组索引</param>
        /// <returns></returns>
        public CreateControlGroup AddOrGetGroup(string group_name, int? group_index)
        {
            var group = list.GetOrAdd(group_name, (k) =>
            {
                CreateControlGroup g = new CreateControlGroup();
                g.Header = k;
                if (group_index.HasValue)
                    g.GroupIndex = group_index.Value;
                return g;
            });
            return group;
        }

        /// <summary>
        /// 尝试获取组
        /// </summary>
        /// <param name="group_name">组名</param>
        /// <param name="group">组对象</param>
        /// <returns></returns>
        public bool TryGetGroup(string group_name, out CreateControlGroup? group)
        {
            return list.TryGetValue(group_name, out group);
        }

        /// <summary>
        /// 获取所有组集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CreateControlGroup> GetGroups()
        {
            return list.Values.OrderBy(it => it.GroupIndex).ThenBy(it => it.GroupId);
        }

        internal DockPanelData AddOrGetDockPanelData(string dock_panel_key, Dock dock, string? parent_key, double width, double height)
        {
            DockPanelData dpdata = dock_panel_list.GetOrAdd(dock_panel_key, (k) =>
            {
                DockPanelData data = new DockPanelData(k);
                data.Dock = dock;
                data.Width = width;
                data.Height = height;
                if(!string.IsNullOrEmpty(parent_key))
                    data.ParentKey = parent_key;
                return data;
            });
            return dpdata;
        }

        internal DockPanelData AddDockPanelDatas(string parent_key, DockPanel root_panel)
        {
            if (!dock_panel_list.TryGetValue(parent_key, out DockPanelData? pdata))
            {
                StringBuilder sb = new StringBuilder();
                if (!string.IsNullOrEmpty(HeaderName))
                    sb.Append($"在 {HeaderName} 中");
                sb.Append($"DockPanelKey为【{parent_key}】的容器不存在");
                throw new ConfigException(sb.ToString());
            }
            if (!pdata.IsCreateDockPanel)
            {
                var tp = pdata.DockPanel;
                if (string.IsNullOrEmpty(pdata.ParentKey))
                {
                    root_panel.Children.Add(tp);
                }
                else
                {
                    var tp2 = AddDockPanelDatas(pdata.ParentKey, root_panel);
                    tp2.DockPanel.Children.Add(tp);
                }
            }

            return pdata;
            
        }
    }
}
